<?php

namespace app\index\controller;

use think\cache\driver\Redis;
use think\Controller;
use think\Request;
use think\Db;

class Basecontroller extends Controller
{

  public $redis = null;
  public $header = null;
  protected $time = 7 * 24 * 60 * 60;
  protected $file_path ='http://121.37.195.155/uploads/'; // 服务器的文件路径地址
  // protected $file_path ='http://localhost/tp5/public/uploads/'; //本地路径地址
    // public $path ='http://localhost/tp5/public/uploads/';



  function __construct()
  {
    $this->redis = new \Redis();
    $this->header = Request::instance()->header();
    $this->redis->connect('127.0.0.1', 6379);
  }

  function notLogin()
  {
    return res(1100, '请先进行登录');
  }
  /**
   * 验证登录函数
   */
  function verifyLogin()
  {
    $header = getheaders();

    if (empty($header['Access-Token'])) {
      return  false;
    }
    $token = $header['Access-Token'];

    $res =  json_decode($this->redis->get($token),true);
    if (!$res) {
      return  false;
    }
    
    return $this->updateStaff($res['staff_id'],$token);
  }

  function verifyLogin_user()
  {
    $token = getheaders()['Access-Token'];
    if (empty($token)) {
      return  false;
    }
    $user = $this->redis->get($token);
    if ($user) {
      return  $this->updateUser($token);
    }
    return false;
  }

  /**
   * 更新redis中的用户信息数据
   */
  function updateUser($token)
  {
    $user = json_decode($this->redis->get($token), true);


    $res =  db('user')->where('uid', $user['uid'])->find();
    $this->redis->set($token, json_encode($res), $this->time);
    return $res;
  }
  /**
   * 获得token中的数据
   */

  function updateStaff($id,$token){
    
    $res =  db('staff')
    ->alias('u')
    ->field('u.* , j.name job_name')
    ->join('gjc_job j','j.jid =u.job_id ')
    ->where('staff_id', $id)
    ->find();
    $this->redis->set($token, json_encode($res), $this->time);
    return $res;


    $res =  db('user')->where('uid', $user['uid'])->find();
    $this->redis->set($token, json_encode($res), $this->time);
    return $res;
  }


}
